package com.sxfans.encryp.digest;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.sxfans.encryp.utils.Convert;

public class MD5Coder {
	
	/**
	 * MD5 16/32摘要处理
	 * @param plainText 处理数据 
	 * @param model
	 */
	public static String md5s(String plainText,int model) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			String buf = Convert.toHex(b);
			return model == 32 ? buf.toString() :  buf.toString().substring(8, 24);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * MD5 16位摘要运算
	 * @param plainText
	 */
	public static String md5s(String plainText){
		return md5s(plainText,16);
	}
	
	

	public static void main(String[] args) throws Exception {
		System.out.println(md5s("abcde",16));
		System.out.println(md5s("abcde",32));
	}
}
